//
//
//
//
// File.h

#ifndef _FILE_H_
#define _FILE_H_

#include <fcntl.h>
#include <sys/stat.h>
#include <string>
#include "datatypes.h"

class File
{
	public:
		enum MODE
		{
			_S_ISUID = S_ISUID,
			_S_ISGID = S_ISGID,
			_S_ISVTX = S_ISVTX,
			_S_ENFMT = S_ENFMT,
			_S_IRUSR = S_IRUSR,
			_S_IWUSR = S_IWUSR,
			_S_IXUSR = S_IXUSR,
			_S_IRGRP = S_IRGRP,
			_S_IWGRP = S_IWGRP,
			_S_IXGRP = S_IXGRP,
			_S_IROTH = S_IROTH,
			_S_IWOTH = S_IWOTH,
			_S_IXOTH = S_IXOTH,
		};

		enum FLAGS
		{
			_O_RDONLY = O_RDONLY,
			_O_WRONLY = O_WRONLY,
			_O_RDWR	 = O_RDWR,

			_O_CREAT		 = O_CREAT,
			_O_EXCL		 = O_EXCL,
			_O_NSHARE	= O_NSHARE,
			_O_RSHARE = O_RSHARE,
			_O_DEFER = O_DEFER,
			_O_NOCTTY = O_NOCTTY,
			_O_TRUNC = O_TRUNC,
			_O_DIRECT = O_DIRECT,
			_O_CIO = O_CIO,
			_O_SNAPSHOT = O_SNAPSHOT,

			_O_SYNC = O_SYNC,
			_O_DSYNC = O_DSYNC,
			_O_RSYNC = O_DSYNC,

			_O_APPEND = O_APPEND,
			_O_DELAY = O_DELAY,
			_O_NDELAY = O_NDELAY,
			_O_NONBLOCK = O_NONBLOCK,
		};

		File(void);
		File(const std::string& path);
		~File();

		std::string Path(void) const;
		void Open( U32 flag, U32 mode);
		void Open(const std::string& path, U32 flag, U32 mode);
		void MapToMem(void*& addr /*OUT*/, size_t size, int prot, int flag, off_t off);
		void Close(void);
		void Stat(struct stat& fill);
		void Stat(const std::string& path, struct stat& fill);

		void Flush(void);
		ssize_t Append(const char* fmt, ...);
		void Remove(void);
		void Unlink(const std::string& path);
		void Unlink(void);
		bool IsOpen(void);
		

		static void Rename(const std::string& src, const std::string& dest);
		static std::string GetSpaceN(U32 count);
		off_t Read(char* szBuf, U32 length);

		friend File& operator << (File& file, const std::string& msg);

		static const char* ENDL;
	private:
		S32 _fd;
		bool _isOpen;
		std::string _path;
};

#endif // _FILE_H_
